package com.rainsoftware.LicenseSystem.services;

import com.rainsoftware.LicenseSystem.models.entity.LicenseKey;
import com.rainsoftware.LicenseSystem.models.entity.Product;
import com.rainsoftware.LicenseSystem.models.entity.SystemUser;
import com.rainsoftware.LicenseSystem.utils.StandardResponse;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

@Service
public class UserInfoService {
    @Autowired
    private Product.Model productModel;
    @Autowired
    private SystemUser.Model sysUserModel;
    @Autowired
    private LicenseKey.Model licenseModel;

    @Autowired
    UserService userService;

    public StandardResponse getUserInfo() {

        int devID = userService.getSystemUser().getId();
        SystemUser user = userService.getSystemUser();
        // get product cnt
        long products = productModel.newQuery().where("developer_id", devID).count();
        long licenseKeys = licenseModel.newQuery().where("developer_id", devID).count();
        long licenseKeys_activated = licenseModel.newQuery().where("developer_id", devID).where("activated", true).count();

        return new StandardResponse(true, HttpStatus.OK, "获取成功",
                new JSONObject().put("name", user.getUsername())
                        .put("products", products)
                        .put("license_keys", licenseKeys)
                        .put("license_keys_activated", licenseKeys_activated));
    }
}
